** Training data **
clear 
use "J:\workdata\706727\Temp\extradata"

keep if inrange(year,2008,2012)
keep if inrange(year-birthyear,18,50)

gen age=year-birthyear

sort pnr year month
foreach var in sick disab sueduc cash_unemp2 cash_unemp cash_int educhelp sometrans dagp_unemp dagp_act ledyd forre re flexjob skaane intyd cash_dagexp  resf jobafk parleave {
qui sum year if !mi(`var') 
replace `var'=0 if mi(`var') & inrange(year,r(min),r(max))

gen byte D`var'=(`var'>0 & !mi(`var')) if inrange(year,r(min),r(max))
drop `var'
}
egen Dcash_any=rowmax(Dcash_unemp2 Dcash_unemp Dcash_int Dintyd Dcash_dagexp Deduchelp)
egen Ddisab_any=rowmax(Dledyd Dforre Dre Dflexjob Dskaane Dresf Djobafk Ddisab )
egen Ddagp_any=rowmax(Ddagp_unemp Ddagp_act)
egen Dallnonparleave=rowmax(Dsick Ddisab Dcash_unemp2 Dcash_unemp Dcash_int Deduchelp Ddagp_unemp Ddagp_act Dledyd Dforre Dre Dflexjob Dskaane Dintyd Dcash_dagexp  Dresf Djobafk)
egen Dmixlabortrans=rowmax(Dforre Dre Dflexjob Dskaane Dresf Djobafk)
drop Dcash_int Deduchelp Dledyd Dintyd Dcash_dagexp Dresf Djobafk

foreach var in sick disab sueduc cash_unemp2 cash_unemp sometrans dagp_unemp dagp_act forre re flexjob skaane parleave cash_any disab_any dagp_any allnonparleave mixlabortrans {

by pnr: gen byte D1`var'=D`var'[_n-1] if pnr==pnr[_n-1]
by pnr: gen byte D2`var'=D`var'[_n-2] if pnr==pnr[_n-2]
by pnr: gen byte D3`var'=D`var'[_n-3] if pnr==pnr[_n-3]

}

/* DEFLATE VARIABLES */
cap program drop deflat
do "I:\Workdata\706727\STATA Programs\deflat.do"
deflat earn 1 year 2020
deflat inc 1 year 2020
deflat kontanthj_13 1 year 2020

replace grad=0 if mi(grad) & year>=2008 & !mi(year)
gen grademp=(grad>80) if !mi(grad)

tab ie_type,gen(immi)
drop immi1 ie_type
gen man=(koen==1)
drop koen

keep pnr year month age man immi* D* earn inc kontanthj_13 grademp 

set seed 112
gen rand=runiform()

keep if rand<0.1
drop rand

save "J:\workdata\706727\Temp\ImputeEmp",replace

** Test data **
clear 
cd "I:\workdata\706727\Build\Data"
use PsykTestLongRunRD, clear

keep if inrange(tyrm,2005.25,2012.25)
keep if inrange(treatagec,15,50)
keep if inrange(distyrm,-6,7)

keep pnr 
duplicates drop

merge 1:m pnr using "J:\workdata\706727\Temp\extradata"
keep if _merge==3
drop _merge

gen age=year-birthyear

sort pnr year month
foreach var in sick disab sueduc cash_unemp2 cash_unemp cash_int educhelp sometrans dagp_unemp dagp_act ledyd forre re flexjob skaane intyd cash_dagexp  resf jobafk parleave {
qui sum year if !mi(`var') 
replace `var'=0 if mi(`var') & inrange(year,r(min),r(max))

gen byte D`var'=(`var'>0 & !mi(`var')) if inrange(year,r(min),r(max))
drop `var'
}
egen Dcash_any=rowmax(Dcash_unemp2 Dcash_unemp Dcash_int Dintyd Dcash_dagexp Deduchelp)
egen Ddisab_any=rowmax(Dledyd Dforre Dre Dflexjob Dskaane Dresf Djobafk Ddisab )
egen Ddagp_any=rowmax(Ddagp_unemp Ddagp_act)
egen Dallnonparleave=rowmax(Dsick Ddisab Dcash_unemp2 Dcash_unemp Dcash_int Deduchelp Ddagp_unemp Ddagp_act Dledyd Dforre Dre Dflexjob Dskaane Dintyd Dcash_dagexp  Dresf Djobafk)
egen Dmixlabortrans=rowmax(Dforre Dre Dflexjob Dskaane Dresf Djobafk)
drop Dcash_int Deduchelp Dledyd Dintyd Dcash_dagexp Dresf Djobafk

foreach var in sick disab sueduc cash_unemp2 cash_unemp sometrans dagp_unemp dagp_act forre re flexjob skaane parleave cash_any disab_any dagp_any allnonparleave mixlabortrans {

by pnr: gen byte D1`var'=D`var'[_n-1] if pnr==pnr[_n-1]
by pnr: gen byte D2`var'=D`var'[_n-2] if pnr==pnr[_n-2]
by pnr: gen byte D3`var'=D`var'[_n-3] if pnr==pnr[_n-3]

}

/* DEFLATE VARIABLES */
cap program drop deflat
do "I:\Workdata\706727\STATA Programs\deflat.do"
deflat earn 1 year 2020
deflat inc 1 year 2020
deflat kontanthj_13 1 year 2020

tab ie_type,gen(immi)
drop immi1 ie_type
gen man=(koen==1)
drop koen

keep pnr year month age man immi* D* earn inc kontanthj_13  

save "J:\workdata\706727\Temp\ImputeEmp_Test",replace
